package com.dxt.service;

import com.dxt.dao.BusiCodeBeanDao;
import com.dxt.dao.CommonDao;
import com.dxt.model.BusiCodeBean;
import com.dxt.model.CommonKeyValueBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class CacheManager {
    private static final Logger logger = LoggerFactory.getLogger(Thread.currentThread().getStackTrace()[1].getClassName());
    @Autowired
    private BusiCodeBeanDao busiCodeBeanDao;

    @Autowired
    private CommonDao commonDao;


    private static ConcurrentHashMap<String, BusiCodeBean> busiMap;
    private static ConcurrentHashMap<String, String> sysConfigMap;
    private static ConcurrentHashMap<String, String> secretKeyMap;

    /**
     * @desc: 刷新缓存,每日凌晨2点更新缓存
     * @return
     */
    @Scheduled(cron = "0 0 02 * * ?")
    public boolean initCache() {
        boolean ret = false;
        try {
            initBusiMap();
            logger.info("加载业务编码完毕");
            initSysConfigMap();
            logger.info("加载系统参数完毕");
            initSecretKeyMap();
            logger.info("加载平台密钥完毕");
            ret = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ret;
    }
    /**
     * @desc:加载业务编码
     */
    private void initBusiMap() {
        try {
            if (null == busiMap) {
                busiMap = new ConcurrentHashMap();
            } else {
                busiMap.clear();
            }
            List<BusiCodeBean> list = busiCodeBeanDao.getAllBusiCodeBeans();
            for (BusiCodeBean busiCodeBean : list) {
                busiMap.put(busiCodeBean.getBusiCode(), busiCodeBean);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @desc:通过业务code获取业务对象
     * @param code
     * @return
     */
    public BusiCodeBean getBusiByCode(String code) {
        if (null == busiMap) {
            initBusiMap();
        }
        return (null != busiMap && null != code && busiMap.containsKey(code) && !""
                .equals(code)) ? busiMap.get(code) : null;
    }

    /**
     * @desc:加载系统参数
     */
    private void initSysConfigMap() {
        try {
            if (null == sysConfigMap) {
                sysConfigMap = new ConcurrentHashMap<>();
            } else {
                sysConfigMap.clear();
            }
            List<CommonKeyValueBean> list = commonDao.getSysConfigBeanList();
            for (CommonKeyValueBean commonKeyValueBean : list) {
                sysConfigMap.put(commonKeyValueBean.getCode(), commonKeyValueBean.getValue());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @desc:加载平台密钥
     */
    private void initSecretKeyMap() {
        try {
            if (null == secretKeyMap) {
                secretKeyMap = new ConcurrentHashMap<>();
            } else {
                secretKeyMap.clear();
            }
            List<CommonKeyValueBean> list = commonDao.getSecretKeyList();
            for (CommonKeyValueBean commonKeyValueBean : list) {
                secretKeyMap.put(commonKeyValueBean.getCode(), commonKeyValueBean.getValue());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @desc:通过key获取参数value
     * @param key
     * @return
     */
    public String getSecretKeyByCode(String key) {
        if (null == secretKeyMap) {
            initSecretKeyMap();
        }
        return (null != secretKeyMap && null != key && secretKeyMap.containsKey(key) && !""
                .equals(key)) ? secretKeyMap.get(key) : null;
    }

    /**
     * @desc:通过key获取参数value
     * @param key
     * @return
     */
    public String getSysConfigByCode(String key) {
        if (null == sysConfigMap) {
            initSysConfigMap();
        }
        return (null != sysConfigMap && null != key && sysConfigMap.containsKey(key) && !""
                .equals(key)) ? sysConfigMap.get(key) : null;
    }

}
